<?php

/**
 * Maikuro CMS
 *
 * LICENSE:
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The license is bundled with this package in the file LICENSE.txt.
 * You may also obtain a copy of the License at this URL:
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @copyright  Copyright (c) 2010, Jonathan Hedrén
 * @author     Jonathan Hedrén <jonathan.hedren@gmail.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
 */

class SiteTag
{

    /**
     *
     * @var FrontendApplicationContext
     */
    private $applicationContext;

    /**
     *
     * @var PageService
     */
    private $pageService;

    /**
     *
     * @param FrontendApplicationContext $applicationContext 
     */
    public function __construct(FrontendApplicationContext $applicationContext, PageService $pageService)
    {
        $this->applicationContext = $applicationContext;
        $this->pageService = $pageService;
    }

    public function ifRootPage()
    {
        $currentPage = $this->applicationContext->getCurrentPage();
        $rootPage = $this->pageService->getRootPage($this->applicationContext->getCurrentSite()->id);
        return ($rootPage->id == $currentPage->id);
    }

    public function displayCustomField(array $params)
    {
        $defaults = array(
            'class' => null,
            'id' => null,
            'name' => null,
            'wraptag' => null
        );

        $params = Utility::arrayOverlay($defaults, $params);

        $site = $this->applicationContext->getCurrentSite();

        if ($site != null && isset($site->customFields[$params['name']])) {
            $customField = $site->customFields[$params['name']];
            return $this->applicationContext
                        ->getTagParser()
                        ->parse($customField->value);
        }
        return null;
    }

    public function displayName(array $params)
    {
        return $this->applicationContext->getCurrentSite()->name;
    }

    public function displayUrl(array $params)
    {
        return $this->applicationContext->getCurrentSite()->url;
    }

    public function getHtmlCloseTag()
    {
        
    }
}